package com.zlq.cn.util;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class IpUtil {
    public static class IpCache {
        private int count;
        private LocalDateTime firstTime;

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public LocalDateTime getFirstTime() {
            return firstTime;
        }

        public void setFirstTime(LocalDateTime firstTime) {
            this.firstTime = firstTime;
        }

        @Override
        public String toString() {
            return "IpCache{" +
                    "count=" + count +
                    ", firstTime=" + firstTime +
                    '}';
        }
    }

    private static final HashMap<String, IpCache> IP_CACHE_MAP = new HashMap<>();
    private static final List<String> BLACK_IP_CACHE_MAP = new ArrayList<>();

    public static boolean checkIp(String ip) {
        if (BLACK_IP_CACHE_MAP.contains(ip))
            return true;
        IpCache ipCache = IP_CACHE_MAP.get(ip);
        if (ipCache == null) {
            ipCache = new IpCache();
            ipCache.setCount(0);
            ipCache.setFirstTime(LocalDateTime.now());
            IP_CACHE_MAP.put(ip, ipCache);
        }
        System.out.println("ip==>" + ip + "<===ipCache==>" + ipCache);
        long minutes = Math.abs(LocalDateTime.now().until(ipCache.firstTime, ChronoUnit.MINUTES));
        if (ipCache.getCount() > 10 && minutes < 5) {// 5分钟内发送了10条
            BLACK_IP_CACHE_MAP.add(ip);
            return true;
        } else {
            if (ipCache.getCount() > 10 && minutes < 5) {// 每10次重置一下时间
                ipCache.setFirstTime(LocalDateTime.now());
            }
            ipCache.setCount(ipCache.getCount() + 1);
        }
        return false;
    }

    private static final String[] HEADERS_TO_TRY = {
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR",
            "X-Real-IP"};

    public static String getClientIpAddress(HttpServletRequest request) {
        String ip = "";
        for (String header : HEADERS_TO_TRY) {
            ip = request.getHeader(header);
            if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
                break;
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
//            if (ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")) {
//                //根据网卡取本机配置的IP
//                InetAddress inet = null;
//                try {
//                    inet = InetAddress.getLocalHost();
//                } catch (UnknownHostException e) {
//                    e.printStackTrace();
//                }
//                ip = inet.getHostAddress();
//            }
        }
        if (ip != null && ip.split(",").length > 1) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }
}
